/*
 * Copyright © 2022 Cask Data, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package io.cdap.plugin.distinct.stepsdesign;

import io.cdap.e2e.utils.CdfHelper;
import io.cdap.e2e.utils.PluginPropertyUtils;
import io.cdap.plugin.distinct.actions.DistinctActions;
import io.cdap.plugin.utils.E2ETestConstants;
import io.cucumber.java.en.Then;
import org.junit.Assert;

/**
 * Distinct Plugin related step design.
 */
public class DistinctAnalytics implements CdfHelper {

  @Then("Validate OUT record count of distinct is equal to IN record count of sink")
  public void validateOUTRecordCountOfDistinctIsEqualToINRecordCountOfSink() {
    Assert.assertEquals(recordOut(), DistinctActions.getTargetRecordsCount());
  }

  @Then("Enter the Distinct plugin fields as list {string}")
  public void enterTheDistinctPluginFieldsAsList(String commaSeparatedFieldList) {
    String[] fields = PluginPropertyUtils.pluginProp(commaSeparatedFieldList).split(",");
    for (int index = 0; index < fields.length; index++) {
      DistinctActions.enterListOfFields(index, fields[index]);
      DistinctActions.clickFieldsAddRowButton(index);
    }
  }

  @Then("Verify Distinct plugin in-line error message for incorrect fields: {string}")
  public void verifyDistinctPluginInLineErrorMessageForIncorrectFields(String fields) {
    DistinctActions.verifyDistinctPluginPropertyInlineErrorMessageForRow
      ("fields", 1,
       PluginPropertyUtils.errorProp(E2ETestConstants.ERROR_MSG_DISTINCT_INVALID_FIELDS)
         .replace("FIELDS", PluginPropertyUtils.pluginProp(fields)));
    DistinctActions.verifyDistinctPluginPropertyInlineErrorMessageForColor("fields", 1);
  }
}
